package com.tosit.bmx.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tosit.bmx.common.json.JSONModel;
import com.tosit.bmx.common.json.JSONUtil;
import com.tosit.bmx.domian.News;
import com.tosit.bmx.domian.Subject;
import com.tosit.bmx.service.admin.WebManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 新闻管理类
 *
 * @author tsl
 * @create 2018-04-26 17:14
 **/
@Controller
@RequestMapping()
public class NewsController {
    @Autowired
    WebManagerService webManagerService;

    /**
     * 获取所有新闻
     *
     * @param
     * @return
     * @author tsl
     * @date 2018/4/26 17:24
     */
    @RequestMapping(value = "/news", method = RequestMethod.GET)
    public void gainNewsAll(HttpServletResponse response) {
        List<News> result = webManagerService.gainNewsAll();
        if (result != null) {
            JSONModel.put("data", result);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 添加新闻
     *
     * @param
     * @return
     * @author tsl
     * @date 2018/4/26 17:24
     */
    @RequestMapping(value = "/news", method = RequestMethod.POST)
    public void addNews(News news,HttpServletResponse response) {
        Integer result = webManagerService.addNew(news);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 删除一个或多个新闻
     *
     * @param ids
     * @return
     * @author tsl
     * @date 2018/4/26 17:24
     */
    @RequestMapping(value = "/news", method = RequestMethod.DELETE)
    public void addNews(@RequestBody List<Integer> ids, HttpServletResponse response) {
        Integer result = webManagerService.removeNews(ids);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 更新新闻信息
     *
     * @param
     * @return
     * @author tsl
     * @date 2018/4/26 17:24
     */
    @RequestMapping(value = "/news", method = RequestMethod.PUT)
    public void modifyNews(News news,HttpServletResponse response) {
        Integer result = null;
        result= webManagerService.modifyNews(news);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }
    /**
     * 关键词或者ID查找新闻
     *@param  keyCode 查询条件
     * @param pageSize
     * @param pageNum
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/seekNews", method = RequestMethod.GET)
    public void seekNews(Integer pageNum, Integer pageSize,String keyCode, HttpServletResponse response) {
        List<News> result = webManagerService.seekNews(keyCode);
        if (result != null) {
            JSONModel.put("data", result);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }
}
